package com.jhx.video.store

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.text.TextUtils
import android.view.WindowManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.jhx.common.framework.QuickBaseActivity
import com.jhx.common.model.EventBean
import com.jhx.common.utils.ImageLoader
import com.jhx.common.utils.UserManager
import com.jhx.video.R
import com.jhx.video.comment.CommentFragment
import com.jhx.video.qualification.QualificationActivity
import com.jhx.video.utils.AppBarStatusChangeListener
import com.jhx.video.videomanager.VideoManagerFragment
import jp.wasabeef.glide.transformations.BlurTransformation
import kotlinx.android.synthetic.main.activity_store.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

/**
 * 店铺主页
 *
 * @author Jack 2018-03-26 16:51
 */
class StoreActivity : QuickBaseActivity() {
    private val mTitleList: ArrayList<String> = ArrayList()
    private val mFragmentList: ArrayList<Fragment> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_store)
        setSupportActionBar(toolbar)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
        if(!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initData()
        initListener()
    }

    private fun initListener() {
        app_bar.addOnOffsetChangedListener(object: AppBarStatusChangeListener() {
            override fun onStateChanged(appBarLayout: AppBarLayout, state: Int) {
                when(state) {
                    AppBarStatusChangeListener.COLLAPSED -> {
                        val user = UserManager.get().user
                        if(user != null && !TextUtils.isEmpty(user.businessName)) {
                            toolbar_title.text = user.businessName
                        }
                    }
                    AppBarStatusChangeListener.EXPANDED -> toolbar_title.text = ""
                    AppBarStatusChangeListener.IDLE -> toolbar_title.text = ""
                }
            }
        })
        img_head.setOnClickListener {
            val intent = Intent()
            intent.action = "com.jhx.usermessage"
            startActivity(intent)
        }
        toolbar.setNavigationOnClickListener {
            pop()
        }
        txt_qualification.setOnClickListener {
            val intent = Intent(this, QualificationActivity::class.java)
            startActivity(intent)
        }
    }

    fun initData() {
        val user = UserManager.get().user
        if(user != null) {
            ImageLoader.get().loadCircleImageByUrl(this, user.headImage, img_head)
            txt_tag.text = user.categoryName
            txt_title.text = user.businessName
            txt_address.text = user.address
            txt_phone.text = user.phone
            Glide.with(this)
                 .load(user.headImage)
                 .bitmapTransform(BlurTransformation(this, 15, 3), CenterCrop(this))
                 .error(R.mipmap.img_default)
                 .into(img_background)
        }
        mTitleList.add("短视频")
        mTitleList.add("店铺照片")
        mTitleList.add("评论")
        tablayout.addTab(tablayout.newTab().setText(mTitleList[0]))
        tablayout.addTab(tablayout.newTab().setText(mTitleList[1]))
        tablayout.addTab(tablayout.newTab().setText(mTitleList[2]))
        mFragmentList.add(VideoManagerFragment())
        mFragmentList.add(PhotoFragment())
        mFragmentList.add(CommentFragment.newInstance(CommentFragment.TYPE_STORE_COMMENT))
        viewpager.adapter = FragmentAdapter(supportFragmentManager)
        viewpager.offscreenPageLimit = 2
        tablayout.setupWithViewPager(viewpager)
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        toolbar.title = ""
    }

    override fun showToolbar(): Boolean {
        return false
    }

    inner class FragmentAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
        override fun getItem(position: Int): Fragment {
            return mFragmentList[position]
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitleList[position]
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    @Subscribe
    fun onEvent(eventBean: EventBean<*>?) {
        if (eventBean != null) {
            if (eventBean.status == EventBean.STATUS_HOME_MSG) {
                val user = UserManager.get().user
                ImageLoader.get().loadCircleImageByUrl(this, user.headImage, img_head)
                txt_title.text = user.businessName
            }
        }
    }
}